<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="overConfigList">
          <el-table-column label="名称" align="center" prop="name"/>
          <el-table-column label="催收天数" align="center" prop="minDay">
            <template slot-scope="scope">
              {{ scope.row.minDay }} - {{ scope.row.maxDay }}
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="400"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleWeight(scope.row)"
              >权重分配
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="addOrUpdateUser(scope.row)"
              >分配
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="最小天数" prop="minDay">
              <el-input v-model="form.minDay" placeholder="请输入最小天数"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="最大天数" prop="maxDay">
              <el-input v-model="form.maxDay" placeholder="请输入最大天数"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="催收分配" :visible.sync="open1" width="600px" append-to-body>
      <el-form ref="form1" :model="form1" label-width="80px">

        <el-row>
          <el-col :span="24">
            <el-form-item label="催收员" prop="userIds">
              <el-checkbox-group v-model="form1.userIds" v-if="overUserList">
                <el-checkbox v-for="item in overUserList" :label="item.userId" size="small">{{ item.userName }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdateForm1">确 定</el-button>
        <el-button @click="open1 = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="权重分配" :visible.sync="open2" width="700px" append-to-body>
      <el-table
        :data="dataList"
        border
        style="width: 100%">
        <el-table-column
          prop="userName"
          label="催收人"
        >
        </el-table-column>
        <el-table-column
          prop="weight"
          label="分配权重"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.weight"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="是否分单"
        >
          <template slot-scope="scope">
            <el-select v-model="scope.row.status">
              <el-option v-for="item in statusList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdateWeight">确 定</el-button>
        <el-button @click="open2 = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listOverConfig,
  getOverConfig,
  addOverConfig,
  updateOverConfig,
  delOverConfig,
  getUserIds,
  updateOverUserIds,
  getOverWeightList,
  saveOrUpdateOverWeight,
  getOverUserList
} from "@/api/app/over";
import {listOverUser} from "@/api/system/user"

export default {
  name: "OverConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      overConfigList: null,
      overUserList: null,
      dataList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      open2: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      form1: {
        userIds: []
      },
      statusList: [
        {
          label: "否",
          value: '0'
        },
        {
          label: "是",
          value: '2'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bankName: undefined
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: "不能为空", trigger: "blur"},
        ],
        minDay: [
          {required: true, message: "不能为空", trigger: "blur"},
        ],
        maxDay: [
          {required: true, message: "不能为空", trigger: "blur"},
        ],
      }
    };
  },
  watch: {},
  created() {
    this.getList();
    // this.getOverUserList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listOverConfig(this.queryParams).then(response => {
          this.overConfigList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // getOverUserList(id) {
    //   listOverUser({id: id}).then(response => {
    //       this.overUserList = response.data;
    //     }
    //   );
    // },
    getOverUserList(id) {
      getOverUserList({id: id}).then(response => {
          this.overUserList = response.data;
          this.open1 = true;
        }
      );
    },

    handleWeight(row) {
      getOverWeightList({overId: row.id}).then(response => {
          this.dataList = response.data;
          this.open2 = true;
        }
      );

    },
    saveOrUpdateWeight() {
      if (!this.dataList) {
        this.open2 = false;
        return false;
      }
      saveOrUpdateOverWeight(this.dataList).then(response => {
        this.open2 = false;
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        minDay: undefined,
        maxDay: undefined
      };
      this.form1 = {
        userIds: []
      };
      this.resetForm("form");
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加催收配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOverConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改催收配置";
      });
    },

    addOrUpdateUser(row) {
      this.reset();
      getUserIds({id: row.id}).then(response => {
        this.form1.userIds = response.data;
        this.form1.id = row.id;
        this.getOverUserList(row.id);
      });
    },

    saveOrUpdateForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          updateOverUserIds(this.form1).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open1 = false;
            this.getList();
          });
        }
      })
    },


    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOverConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOverConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return delOverConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
